<!DOCTYPE html>
<html>
<head>
    <title>井字棋游戏</title>
    <style>
        body {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            font-family: Arial, sans-serif;
            min-height: 100vh;
            margin: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: white;
        }

        .container {
            background: rgba(255, 255, 255, 0.1);
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
            backdrop-filter: blur(4px);
        }

        .title {
            font-size: 36px;
            margin-bottom: 20px;
            text-align: center;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

        .board {
            display: grid;
            grid-template-columns: repeat(3, 100px);
            gap: 5px;
            margin: 20px auto;
            width: 310px;
            background: rgba(255, 255, 255, 0.1);
            padding: 10px;
            border-radius: 10px;
        }

        .cell {
            width: 100px;
            height: 100px;
            border: 2px solid rgba(255, 255, 255, 0.5);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 40px;
            cursor: pointer;
            color: white;
            background: rgba(255, 255, 255, 0.1);
            transition: all 0.3s ease;
        }

        .cell:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: scale(1.05);
        }

        .message {
            text-align: center;
            font-size: 24px;
            margin: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
            min-height: 36px;
        }

        .reset-btn {
            display: block;
            margin: 20px auto;
            padding: 12px 30px;
            font-size: 18px;
            cursor: pointer;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 25px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }

        .reset-btn:hover {
            background: #45a049;
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
        }

        .cell[data-value="X"] {
            color: #FF5252;
            text-shadow: 0 0 10px rgba(255, 82, 82, 0.5);
        }

        .cell[data-value="O"] {
            color: #2196F3;
            text-shadow: 0 0 10px rgba(33, 150, 243, 0.5);
        }

        @media (max-width: 600px) {
            .container {
                padding: 15px;
            }
            .board {
                width: 250px;
                gap: 3px;
            }
            .cell {
                width: 80px;
                height: 80px;
                font-size: 32px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="title">井字棋游戏</h1>
        <div class="message" id="message">你是X，AI是O。点击开始游戏！</div>
        <div class="board" id="board"></div>
        <button class="reset-btn" onclick="resetGame()">重新开始</button>
    </div>

    <script>
        let gameOver = false;

        function createBoard() {
            const board = document.getElementById('board');
            board.innerHTML = '';
            for (let i = 0; i < 3; i++) {
                for (let j = 0; j < 3; j++) {
                    const cell = document.createElement('div');
                    cell.className = 'cell';
                    cell.onclick = () => makeMove(i, j);
                    cell.dataset.row = i;
                    cell.dataset.col = j;
                    board.appendChild(cell);
                }
            }
        }

        function updateBoard(board) {
            const cells = document.getElementsByClassName('cell');
            for (let i = 0; i < 3; i++) {
                for (let j = 0; j < 3; j++) {
                    const idx = i * 3 + j;
                    const value = board[i][j] === 1 ? 'X' : 
                                board[i][j] === -1 ? 'O' : '';
                    cells[idx].textContent = value;
                    cells[idx].dataset.value = value;
                }
            }
        }

        async function makeMove(row, col) {
            if (gameOver) return;

            const response = await fetch('/make_move', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ row, col })
            });

            const data = await response.json();
            
            if (data.error) {
                document.getElementById('message').textContent = data.error;
                return;
            }

            updateBoard(data.board);

            if (data.game_over) {
                gameOver = true;
                if (data.winner === 1) {
                    document.getElementById('message').textContent = '恭喜你赢了！';
                } else if (data.winner === -1) {
                    document.getElementById('message').textContent = 'AI赢了！';
                } else {
                    document.getElementById('message').textContent = '平局！';
                }
            }
        }

        async function resetGame() {
            const response = await fetch('/reset', {
                method: 'POST'
            });
            const data = await response.json();
            updateBoard(data.board);
            gameOver = false;
            document.getElementById('message').textContent = '你是X，AI是O。点击开始游戏！';
        }

        // 初始化游戏
        createBoard();
    </script>
</body>
</html>